package com.canbot.u05.activity.customvoice

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.alibaba.fastjson.JSON
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.customvoice.adapter.CustomDialogDetailsAdapter
import com.canbot.u05.activity.customvoice.bean.CustomDialogExcelDatas
import com.canbot.u05.activity.customvoice.tool.CustomDialogDetailsView
import com.canbot.u05.activity.customvoice.tool.CustomDialogView
import com.canbot.u05.activity.overridePendingTransitionExt
import com.canbot.u05.activity.showToast
import com.canbot.u05.bean.JsonResponse
import com.canbot.u05.socket.IpMsgConst
import com.canbot.u05.socket.MsgSendUtils
import com.canbot.u05.socket.MsgType
import com.canbot.u05.utils.OkHttpUtils
import com.canbot.u05.utils.PathConst
import com.canbot.u05.widget.LoadingDialogUtils
import kotlinx.android.synthetic.main.activity_custom_dialog_details.*
import java.util.HashMap

/**
 * CustomDialogDetailsActivity
 *
 * @author  Liu.Heng
 * @date  2019/5/29
 */
class CustomDialogDetailsActivity : BaseActivityNoTitle() {

    private lateinit var customDialogExcelDatas: CustomDialogExcelDatas

    private lateinit var dialogLoading: Dialog

    private lateinit var customDialogDetailsAdapter: CustomDialogDetailsAdapter

    private lateinit var questionList: MutableList<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_custom_dialog_details)
        initView()
    }

    override fun onResume() {
        super.onResume()
        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.CUSTOM_DIALOG_PAYH + "u05_dhxq_001.wav")
        MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "12")
    }

    @SuppressLint("SetTextI18n")
    private fun initView() {
        val intent = intent
        customDialogExcelDatas = JSON.parseObject(intent.getStringExtra("data"), CustomDialogExcelDatas::class.java)
        questionList = JSON.parseArray(intent.getStringExtra("questions"), String::class.java)
        details_back.setOnClickListener(::onClick)
        details_change.setOnClickListener(::onClick)
        details_del.setOnClickListener(::onClick)
        details_relativeLayout.setOnClickListener(::onClick)
        size_text.text = "共${customDialogExcelDatas.listQsBean.size}个问题"
        first_question_text.text = customDialogExcelDatas.listQsBean[0].question
        details_recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        customDialogDetailsAdapter = CustomDialogDetailsAdapter(this, customDialogExcelDatas.listAsBean)
        details_recyclerView.adapter = customDialogDetailsAdapter
    }

    private fun onClick(view: View) {

        when (view.id) {
            /**返回*/
            R.id.details_back -> {
                finish()
                overridePendingTransitionExt()
            }
            /**修改*/
            R.id.details_change -> {
                if (questionList.isNotEmpty()) {
                    customDialogExcelDatas.listQsBean.forEach {
                        questionList.remove(it.question)
                    }
                }
                val intent = Intent(this, CustomDialogAddQuestionActivity::class.java)
                intent.putExtra("data", JSON.toJSONString(customDialogExcelDatas))
                intent.putExtra("questions", JSON.toJSONString(questionList))
                startActivity(intent)
                overridePendingTransitionExt()
            }
            /**删除*/
            R.id.details_del -> {
                delDialogs()
            }
            /**查看问题列表*/
            R.id.details_relativeLayout -> {
                val customDialogDetailsView = CustomDialogDetailsView(this, customDialogExcelDatas)
                customDialogDetailsView.show()
            }
            else -> {
            }
        }
    }

    private fun delDialogs() {

        CustomDialogView(this, object : CustomDialogView.OnCloseListener {
            override fun onClick(dialog: Dialog?, confirm: Boolean) {
                if (confirm) {
                    dialog?.dismiss()
                    val mDelList: MutableList<CustomDialogExcelDatas> = ArrayList()
                    mDelList.add(customDialogExcelDatas)
                    dialogLoading = LoadingDialogUtils.createLoadingDialog(this@CustomDialogDetailsActivity, "正在删除中...")
                    dialogLoading.show()
                    val url = "http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "dialogcustom"
                    val hashMap = HashMap<String, String>()
                    hashMap["type"] = "1"
                    hashMap["delDialogs"] = JSON.toJSONString(mDelList)
                    OkHttpUtils.upLoadFile(url, hashMap, object : OkHttpUtils.OkHttpCallBack {
                        @SuppressLint("LongLogTag")
                        override fun onSuccess(result: String) {
                            val strMsgBean = JSON.parseObject(result, JsonResponse::class.java)
                            if (strMsgBean.rc == 0) {
                                runOnUiThread {
                                    dialogLoading.dismiss()
                                    showToast(getString(R.string.deleted_successfully))
                                    val intent = Intent(this@CustomDialogDetailsActivity, CustomDialogMainActivity::class.java)
                                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                                    startActivity(intent)
                                    finish()
                                    overridePendingTransitionExt()
                                }
                            }
                        }

                        override fun onFailure() {
                            runOnUiThread {
                                dialogLoading.dismiss()
                                showToast(getString(R.string.delete_failed))
                            }
                        }
                    })
                }
            }
        }, "提示", "是否删除这条对话").show()

    }
}
